/*
	|=====|	|	|
	|	|	|	|
	|=====|	|=====|
	|	|	|	|
	|=====|	|	|
	
	BASEHACK
	Code by "Brwarner" STEAM_0_1_12243023
	
	Clientside HUD drawing functions
*/


//CLient known phase information
local phaseNum = 0;
local phaseTime = 0;

local block_counts = {0, 0};

//Called when timeChange usermessage recieved
function ChangePhaseTime( arg )
	//Update information
	phaseNum = arg:ReadLong();
	phaseTime = arg:ReadLong();
end
usermessage.Hook("timeChange", ChangePhaseTime);

//Called to update block counts
function UpdateBlockCounts( args )
	//Update our info
	block_counts[1] = args:ReadLong();
	block_counts[2] = args:ReadLong();
end
usermessage.Hook("UpdateLimit", UpdateBlockCounts);

//Draws the Phase and Time HUD
function HUDPhase()
	//Hud DATA
	local hudWidth = 100;
	local hudHeight = 75;
	local hudX = surface.ScreenWidth()-5-hudWidth;
	local hudY = 5;
	
	//Draw a rounded box for showing the phase
	draw.RoundedBox(15, hudX, hudY, hudWidth, hudHeight, Color(51, 58, 51, 150) );
	local phaseName = "";
	
	//Get current phase name
	if(phaseNum == 0) then phaseName = "Build";
	else phaseName = "Combat"; end
	
	//Draw texts
	draw.SimpleText(phaseName, "ScoreboardText", hudX+5, hudY + hudHeight-20, Color(255, 255, 0, 255) );
	draw.SimpleText(tostring(phaseTime), "ScoreboardText", hudX+50, hudY+5, Color(255, 255, 0, 255) );
end

//Draw the block health HUD
function HUDBlockHealth()
	//Get block in range
	local tr = LocalPlayer():GetEyeTrace();
	
	//If it isn't valid, a block or otherwise uneeded, return
	if(!tr.Entity) then return; end
	if(!tr.Entity:IsValid()) then return; end
	if(tr.Entity:GetClass() != "bh_block") then return; end
	local ent = tr.Entity;
	
	//HUD DATA
	local hudX = 5;
	local hudY = 5;
	local hudWidth = 200;
	local hudHeight = 50;
	
	//Get the maxmimum health of the SENT
	local maxHealth = tr.Entity.block.Health;
	
	//Find out if it's hacked or not, and if they are alound to see it
	local showHack = false;
	if(tr.Entity:GetNetworkedBool("isHack") && tr.Entity.block.Team != LocalPlayer():Team()) then 
		showHack = true; 
	end
	
	//Draw box and tex showing this
	local h = math.Round(ent:GetNetworkedInt("blockHealth"));
	draw.RoundedBox(15, hudX, hudY, hudWidth, hudHeight, Color(51, 58, 51, 150) );
	draw.SimpleText("Health: " .. tostring(h) .. "/" .. tostring(maxHealth), 
		"ScoreboardText", hudX + 5, hudY + 5, team.GetColor(tr.Entity.block.Team));
	if(showHack) then draw.SimpleText("HACKED!", "ScoreboardText", hudX+5,hudY+25, team.GetColor(LocalPlayer():Team()));
		end
end

function HUDBlockCount()
	//HUD DATA
	local hudX = ScrW()/2-50;
	local hudY = 5;
	local hudWidth = 100;
	local hudHeight = 50;
	
	local count = block_counts[LocalPlayer():Team()];
	
	draw.RoundedBox(15, hudX, hudY, hudWidth, hudHeight, Color(51, 58, 51, 150) );
	draw.SimpleText("Blocks: " .. tostring(count) .. "/" .. tostring(maxBlocks), "ScoreboardText",
		hudX+5, hudY+5, team.GetColor(LocalPlayer():Team()));
end

//Draws our hud elements
function DrawHUDS()
	HUDBlockHealth();
	HUDPhase();
	HUDBlockCount();
end
hook.Add("HUDPaint", "BHHUDS", DrawHUDS);